package org.redr.widgetGUI.settings;

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.redr.widgetGUI.settings.settingsTools.ItemsHandler;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public abstract class XMLSetting {
	
	public Element e;
	public String defaultName = "Setting";
	
	
	public XMLSetting setDefault(String id, String setting){
		if (_hasSetting(id)) return this; // don't do anything if there is already the setting
		Element newSetting	= 	e.getOwnerDocument().createElement(defaultName);
		e.appendChild(newSetting);
		newSetting.setAttribute("id", id);
		newSetting.setAttribute("state", setting);
		return this;
	}
	
	public ItemsHandler items(String group){
		int index = itemsIndex(group);
		if (index < 0){
			Element ig = e.getOwnerDocument().createElement("Items");
			ig.setAttribute("key", group);
			e.appendChild(ig);
			return new ItemsHandler(ig);
		} 
		else {
			NodeList nl = e.getElementsByTagName("Items");
			return new ItemsHandler((Element) nl.item(index));
		} 
	}
	
	public int itemsIndex(String key){
		NodeList nl = e.getElementsByTagName("Items");
		for (int n = 0; n < nl.getLength(); n++){
			Element el = (Element) nl.item(n);
			if (el.getAttribute("key").equals(key)) return n;
		}
		return -1;
	}
	
	protected int _settingIndex(String id){
		NodeList nl = e.getElementsByTagName(defaultName);
		for (int n = 0 ; n < nl.getLength(); n++){
			Element el	=	(Element) nl.item(n);
			if	(el.getAttribute("id").equals(id)) return n;
		}
		return -1;
	}
	
	protected boolean _hasSetting(String id){
		if (_settingIndex(id) < 0) return false;
		else return true;
	}
	
	public String getSetting(String id){
		NodeList nl = e.getElementsByTagName(defaultName);
		for (int n = 0; n < nl.getLength(); n++){
			Element el	=	(Element) nl.item(n);
			if (el.getAttribute("id").equals(id)) return el.getAttribute("state");
		}
		return null;
	}
	
	public XMLSetting setSetting(String key, String value){
		int index = _settingIndex(key);
		
		//new setting
		if (index < 0){
			Element newSetting	= 	e.getOwnerDocument().createElement(defaultName);
			e.appendChild(newSetting);
			newSetting.setAttribute("id", key);
			newSetting.setAttribute("state", value);
		} else {
			NodeList nl = e.getElementsByTagName("Setting");
			Element el = (Element) nl.item(index);
			el.setAttribute("state", value);
		}
		return this;
	}
	
	protected Element getChildElement(String type, String id){
		NodeList nl = e.getElementsByTagName(type);
		if (nl.getLength() == 0) return null;
		for (int n = 0; n < nl.getLength(); n++){
			Element e = (Element) nl.item(n);
			if (id != null){
				if (e.hasAttribute("id") & e.getAttribute("id").equals(id)) return e;
			} else return e;
		}
		return null;
	}
	
	public static final String xmlToString(Node n){
		StringWriter sw = new StringWriter();
	    try {
	      Transformer t = TransformerFactory.newInstance().newTransformer();
	      t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
	      t.setOutputProperty(OutputKeys.INDENT, "yes");
	      t.transform(new DOMSource(n), new StreamResult(sw));
	    } catch (TransformerException te) {
	      System.out.println("nodeToString Transformer Exception");
	    }
	    return sw.toString();
	}
}
